<?php
function list_adjustment(){
	//PASANG JAVASCRIPT
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');	

	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Komponen'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Tipe'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'Reset'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	if(isset($_GET['key']))
		$query = "SELECT  * FROM pr_penyesuaian a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id 
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR b.nama_komponen_gaji LIKE '%{$_GET['key']}%'  )
			";
	else
		$query = " SELECT * FROM pr_penyesuaian 
			WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		";	

	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$editproperty = array(
						'href'=>'index.php?com='.$_GET['com'].'&task=edit_pengecualian&id='.$ey['id'] ,
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:; ',
			'onclick'=>'javascript:confirmDelete('.$ey['id'].');',
			'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$komponens=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$ey['komponen_id']);
		$tipes = array(
				'1'=>'Tunjangan',
				'2'=>'Potongan',
				'3'=>'Iuran',
			);
		
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'nama'=>  $karyawan['nama_gelar'] , 
			'komponen'=>  $komponens['nama_komponen_gaji'],
			'tipe'=>  $tipes[$komponens['tipe']],
			'nominal'=>   position_text_align (rp_format($ey['penyesuaian'] ) ,'right'),    
			'reset'=>   position_text_align ($ey['reset']  ,'center'),    
			'operasi'=> position_text_align( $edit_button.' '. $delete_button, 'right'),
		); 
	}

	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	
	$view  ='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"> '.get_tanggal_by_periode_id().'</span>'   , "<b>Periode Gaji</b>"    ); 
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Pencarian cepat</b></span>
	<input type="hidden" name="task" value="detail_karyawan_list"/>
	<input type="hidden" name="komponen_gaji_id" value="'.$_GET['komponen_gaji_id'].'"/>
	<br/><input type="text"  style="width:200px;" name="key" value="'.(isset($_GET['key']) ? $_GET['key']: "").'" />
	<input class="simple_search" type="submit" value=" CARI " /> </form>';

	$menuontop = array(  
	//	'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&field='.$field.'&key='.$key.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'upload'=>array('onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pr_pengecualian&task=upload_pengecualian\' });'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&field='.$field.'&key='.$key.'\';'),
		'Tambah Data'=>array(
			'onclick'=>'javascript:jQuery.facebox({ ajax: \'index.php?com=pr_pengecualian&task=edit_pengecualian\' });'),
		);
	$printmenu = additional_menu_on_list( $menuontop , $search);
	$paging = $kgPagerOBJ ->showPaging();
	return  $view.$printmenu.table_builder($header , $datas ,  6 , false , $paging  );

}


function upload_exception(){
	 
	$dest ='files/csv/file_exception_'.$_POST['komponen_id'].'_'.date('Ymd_his').'.csv';
	$ori = $_FILES['csvfile']['tmp_name'];
	$upload = move_uploaded_file($ori , $dest); 
	$n=0;
	if($upload){
		$handle = fopen($dest, "r"); 
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			if(trim($data[0])<>'' && $data[1]<> ''){
			$karyawan_id =check_karyawan_by_nik(sprintf('%07d',$data[0]));
			stor_temp_table_pengecualian($karyawan_id, $data[1] );
			$n++;
			}
		}
		fclose($handle);
	}
	return $n ;
}


function stor_temp_table_pengecualian($kid, $value ){ 
	$cek = isset($_POST['reset']) ? 'Y' : 'T'  ;
	$datas = array(
		'karyawan_id'=>my_type_data_int($kid), 
		'komponen_id'=>my_type_data_int($_POST['komponen_id']), 
		'penyesuaian'=>my_type_data_int($value),
		'reset'=>my_type_data_str($cek),
	);
	my_insert_record('pr_penyesuaian',$datas);
}

function form_upload_pengecualian(){
	$view  = form_header( "detail pegawai" , "cp"  );
	
	$csvfile = array(
		'name'=>'csvfile',
		'id'=>'csvfile',
		'type'=>'file',
		'size'=>'45'
	);
	$form_csvfile = form_dynamic($csvfile);
	$view .= form_field_display( $form_csvfile  , "<b>CSV File</b>"  );
	 
	$checkKomponen = "SELECT * FROM pr_komponen_gaji ORDER BY tipe ASC";
	$cresult = my_query($checkKomponen);
	$optionKomponen = array();
	while( $row = my_fetch_array($cresult) ){ 	
		$optionKomponen[$row['komponen_gaji_id']] = $row['nama_komponen_gaji'];
	}
	$komponen = array(
		'name'=>'komponen_id',
		'value'=>(isset($_POST['komponen_id'])? $_POST['komponen_id'] :  $fields['komponen_id']	     ),
		'id'=>'komponen_id'
	);
	$view .= form_field_display( '<span class="label_form">'.form_dropdown($komponen , $optionKomponen).'</span>'   , "<b>Nama Komponen</b>"    ); 

	if($fields['reset']=='Y'){ 
		$reset = array(
			'name'=>'reset',
			'id'=>'reset',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$reset = array(
			'name'=>'reset',
			'id'=>'reset',
			'type'=>'checkbox',
		); 
	}
	$form_reset = form_dynamic($reset);
	$view .= form_field_display( $form_reset , "Reset" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
	return $view;
}


function form_pengecualian($id=0){
	$fields = my_get_data_by_id('pr_penyesuaian','id',$id);
 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');	
	 
	$penyesuaian = array(
		'name'=>'penyesuaian',
		'id'=>'penyesuaian',
		'size'=>'15',
		'value'=>($fields ?  rp_format($fields['penyesuaian'])  :'')
	);
	$form_penyesuaian = form_money($penyesuaian); 
	
	$karyawan_data = loaddata_karyawan($fields['karyawan_id']);
	$jabatan = my_get_data_by_id('jabatan','jabatan_id', ($karyawan['jabatan_id']<>'' ? $karyawan['jabatan_id']: 0));
	$view  = form_header( "detail pegawai" , "cp"  );

	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : ( $fields['karyawan_id'] > 0 ? $karyawan_data['nik'].'/'.$karyawan_data['nama'] :'') ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "<b>NIK/Nama Kary.</b>"  );
	
	$checkKomponen = "SELECT * FROM pr_komponen_gaji ORDER BY tipe ASC";
	$cresult = my_query($checkKomponen);
	$optionKomponen = array();
	while( $row = my_fetch_array($cresult) ){ 	
		$optionKomponen[$row['komponen_gaji_id']] = $row['nama_komponen_gaji'];
	}
	$komponen = array(
		'name'=>'komponen_id',
		'value'=>(isset($_POST['komponen_id'])? $_POST['komponen_id'] :  $fields['komponen_id']	     ),
		'id'=>'komponen_id'
	);
	$view .= form_field_display( '<span class="label_form">'.form_dropdown($komponen , $optionKomponen).'</span>'   , "<b>Nama Komponen</b>"    ); 
	$view .= form_field_display(  $form_penyesuaian  , "<b>Nominal</b>"    );

	if($fields['reset']=='Y'){ 
		$reset = array(
			'name'=>'reset',
			'id'=>'reset',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$reset = array(
			'name'=>'reset',
			'id'=>'reset',
			'type'=>'checkbox',
		); 
	}
	$form_reset = form_dynamic($reset);
	$view .= form_field_display( $form_reset , "Reset" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
	return $view;
}

function submit_pengecualian($karyawan_id , $komponen_id){

	$query = "SELECT * FROM pr_penyesuaian 
		WHERE karyawan_id = {$karyawan_id} 
		AND komponen_id={$komponen_id}";
	$result = my_query($query);
	if(my_num_rows($result) > 0){
		$query = "UPDATE pr_penyesuaian SET 
			penyesuaian = '".$_POST['penyesuaian']."' 
			WHERE karyawan_id=".$karyawan_id."
			AND komponen_id=".$komponen_id ;
		return my_query($query);	
	}	
	$reset = isset($_POST['reset']) ? 'Y': 'T';

	$datas = array(
		'karyawan_id'	=> my_type_data_int($karyawan_id ),
		'komponen_id'	=> my_type_data_int( $komponen_id),
		'penyesuaian'	=> my_type_data_str(set_text_as_number($_POST['penyesuaian'])),
		'reset'			=> my_type_data_str($reset),
	);	
	return my_insert_record( 'pr_penyesuaian' , $datas);
}
function update_pengecualian_submit($id,$karyawan_id){
	$reset = isset($_POST['reset']) ? 'Y': 'T';
	$datas = array(
		'karyawan_id'	=> my_type_data_int($karyawan_id ),
		'komponen_id'	=> my_type_data_int( $_POST['komponen_id']),
		'penyesuaian'	=> my_type_data_str(set_text_as_number($_POST['penyesuaian'])),
		'reset'	=> my_type_data_str($reset),
	);	
	return my_update_record( 'pr_penyesuaian' , 'id',$id,$datas);
}

function get_tanggal_by_periode_id(){
	$periode_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periodes['tanggal']));
	$tahun = $periodes['tahun'] ;
	return sprintf( '%02d',$bulan).'-'.$tahun ;
}

function delete_pengecualian($id){
	$query = "DELETE FROM pr_penyesuaian WHERE id={$id}";
	return my_query($query);
}

function print_plain(){
	if(isset($_GET['key']))
		$query = "SELECT  * FROM pr_penyesuaian a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id 
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR b.nama_komponen_gaji LIKE '%{$_GET['key']}%'  )
			";
	else
		$query = " SELECT * FROM pr_penyesuaian 
			WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		";	


	$result = my_query($query ); 
	$datas = array();  
	$i=1;
	$datas[] = array(
		 'k1'=>array("NIK" ,10 ,STR_PAD_BOTH),
		 'k2'=>array("Nama" ,30,STR_PAD_RIGHT),
		 'k3'=>array("Komponen" ,30,STR_PAD_RIGHT),
		 'k4'=>array("Tipe" ,10,STR_PAD_LEFT),  
		 'k5'=>array("Nominal" ,10,STR_PAD_BOTH),  
		 'k6'=>array("Reset" ,10,STR_PAD_BOTH),  
	); 
	$datas[] = array(	//FOR HEADER SPACE (NECK)
		 'k1'=>array(" " ,10 ,STR_PAD_BOTH),
		 'k2'=>array(" " ,30,STR_PAD_RIGHT),
		 'k3'=>array(" " ,30,STR_PAD_RIGHT),
		 'k4'=>array(" " ,10,STR_PAD_LEFT),  
		 'k5'=>array(" " ,10,STR_PAD_LEFT),  
		 'k6'=>array(" " ,10,STR_PAD_LEFT),  
	); 
	$i=0;
	
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$komponens=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$ey['komponen_id']);
		$tipes = array(
				'1'=>'Tunjangan',
				'2'=>'Potongan',
				'3'=>'Iuran',
			);


		$datas[] = array(		 
			'k1'=>array($karyawan['nik'] ,10 ,STR_PAD_BOTH),
			'k2'=>array($karyawan['nama_gelar'],30,STR_PAD_RIGHT),
			'k3'=>array( $komponens['nama_komponen_gaji'] ,30,STR_PAD_RIGHT),
			'k4'=>array( $tipes[$komponens['tipe']] ,10,STR_PAD_RIGHT),
			'k5'=>array(rp_format($ey['penyesuaian']),10,STR_PAD_LEFT),  
			'k6'=>array($ey['reset'] ,10,STR_PAD_BOTH),  
		);
	}
	$view = 	str_pad("CETAK PENGECUALIAN",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}


function excel_report(){
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'border-bottom:2px solid;width:30%'),  
		'Komponen'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Tipe'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'Reset'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
	);
	if(isset($_GET['key']))
		$query = "SELECT  * FROM pr_penyesuaian a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_id=b.komponen_gaji_id 
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' OR b.nama_komponen_gaji LIKE '%{$_GET['key']}%'  )
			";
	else
		$query = " SELECT * FROM pr_penyesuaian 
			WHERE karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
		";	


	$result = my_query($query ); 
	$row = array();  
	while($ey = my_fetch_array($result)){
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$komponens=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$ey['komponen_id']);
		$tipes = array(
				'1'=>'Tunjangan',
				'2'=>'Potongan',
				'3'=>'Iuran',
			);
		$row[] = array(
			'nik'=> $karyawan['nik'] ,    
			'nama'=>  $karyawan['nama_gelar'] , 
			'komponen'=>  $komponens['nama_komponen_gaji'],
			'tipe'=>  $tipes[$komponens['tipe']],
			'nominal'=>   position_text_align ($ey['penyesuaian']  ,'right'),    
			'reset'=>   position_text_align ($ey['reset']  ,'center'),    
		); 
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}